package com.pap.rbac.userextra.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.date.DateUtils;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.base.util.string.StringUtilss;
import com.pap.obj.vo.business.KeyOperationValueEnum;
import com.pap.obj.vo.page.Page;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.rbac.agent.user.IUserAgent;
import com.pap.rbac.agent.userextra.IUserExtraPropAgent;
import com.pap.rbac.agent.userextra.dto.UserExtraPropWithExtraDTO;
import com.pap.rbac.user.auto.entity.User;
import com.pap.rbac.userextra.auto.entity.UserExtraProp;

@RestController
@RequestMapping("/v1/rbac/userextraprop")
public class UserExtraPropController extends BaseController<UserExtraProp, IUserExtraPropAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(UserExtraPropController.class);
	
	@Resource(name = "userExtraPropAgent")
	@Override
	public void setService(IUserExtraPropAgent agent) {
		super.service = agent;
	}
	
	@Autowired
	private IUserAgent userAgent;
	
	@Override
	@RequestMapping(value = "/add")
	public ResponseVO<UserExtraProp> add(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "addJSON", required = true) String addJSON) throws Exception {
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		UserExtraProp e = objectMapper.readValue(addJSON, UserExtraProp.class);
		if(StringUtilss.isEmpty(e.getId())) {
			e.setId(UUID.randomUUID().toString());
		}
		e.setCreateIp("0.0.0.0");
		e.setCreateTime(DateUtils.getCurrDateTimeStr());
		e.setCreateUser(loginUserId);
		e.setClientLicenseId(clientLicenseId);

		int operationInt = service.insertSelective(e);
		return ResponseVO.successdata(operationInt);
	}

	@Override
	@SuppressWarnings("unchecked")
	@RequestMapping("/querypage")
	public ResponseVO<UserExtraProp> queryPage(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "queryJSON", required = true) String queryJSON,
			@RequestParam(value = "pageJSON", required = true) String pageJSON) throws Exception {

		ObjectMapper mapper = JacksonUtilss.getObjectMapper();
		
		Map<Object, Object> searchMap = new HashMap<Object, Object>();
		
		Map<Object, Object> queryMap = mapper.readValue(queryJSON, Map.class);
		Map<Object, Object> pageMap = mapper.readValue(pageJSON, Map.class);
		
		searchMap.putAll(queryMap);
		
		Page page = null;
		if(pageMap != null && !pageMap.isEmpty() && pageMap.containsKey("page") && pageMap.containsKey("rows")) {
			page = new Page(pageMap.get("page").toString(), pageMap.get("rows").toString(), 0);
			searchMap.put("page", page);
		} else {
			page = new Page("1", "10", 0);
		}
		searchMap.put("clientLicenseId", clientLicenseId);
		
		List<UserExtraPropWithExtraDTO> dataWithParent = new ArrayList<UserExtraPropWithExtraDTO>();
		
		List<UserExtraProp> operationList =  service.selectListByMap(searchMap);
		if(operationList != null && operationList.size() > 0) {
			for (UserExtraProp userExtraProp : operationList) {
				UserExtraPropWithExtraDTO dtoTemp = new UserExtraPropWithExtraDTO();
				BeanUtils.copyProperties(userExtraProp, dtoTemp);
				User temp = userAgent.selectByPrimaryKey(userExtraProp.getUserId());
				if(temp != null) {
					dtoTemp.setUserName(temp.getName());
				}
				
				KeyOperationValueEnum keyOperationValueEnum = KeyOperationValueEnum.getKeyOperationValueEnumByKey(userExtraProp.getPropOper());
				if(keyOperationValueEnum != null) {
					dtoTemp.setPropOperName(keyOperationValueEnum.getValue());
				}
				dataWithParent.add(dtoTemp);
			}
		}
		
		
		int count = service.selectCountByMap(searchMap);
		page = new Page(page.getCurrent() + "", page.getLength() + "", count);
		
		return ResponseVO.successdatas(dataWithParent, page);
	}
}
